/**
 * Created by zhuyufeng on 2016/1/8.
 */
var classStore = Ext.create('Ext.data.Store', {
	fields: ['class'],
	data : [
		{"class":"校园一卡通"},
		{"class":"身份证"}
	]
});
Ext.define('outsiderModel', {
	extend: 'Ext.data.Model',
	fields: [ 'name','class','number','time','mark']
});
var outsiderStore = Ext.create('Ext.data.Store', {
	model: 'outsiderModel',
	autoLoad: true,
	pageSize: 10,
	proxy: {
		type: 'ajax',
		url : './json/outsider',
		reader: {
			type: 'json',
			root: 'rows',
			totalProperty: 'total'
		}
	}
});
var sel;
function del(obj){
	if(obj == ''){
		Ext.MessageBox.alert('提示','请选中要删除的记录');
	}else {
		for (var p in obj) {
			outsiderGrid.store.remove(obj[p]);
		}
	}
}
var outsiderGrid = Ext.create('Ext.grid.Panel', {
	region:'center',
	split:false,
	border:true,
	collapsible:false,
	activeTab:0,
	autoHeight:true,
	store: outsiderStore,
	height:280,
	forceFit:true,
	selType: 'checkboxmodel',
	selModel: new Ext.selection.CheckboxModel({
		injectCheckbox:0,//checkbox位于哪一列，默认值为0
		mode:'simple',//multi,simple,single；默认为多选multi
		allowDeselect:true,//如果值true，并且mode值为单选（single）时，可以通过点击checkbox取消对其的选择
		checkOnly:false,//当checkOnly的值为true时只能从前面的复选框来选中本行，为false则可以
		enableKeyNav:false,
		listeners: {
			deselect: function (model, record, index) {//取消选中时产生的事件
			},
			select: function (model, record, index) {//record被选中时产生的事件
			},
			selectionchange: function (model, selected) {//选择有改变时产生的事件
				var records = model.getSelection();
				if (records != '') {
					for (var i in records) {
						records[i].get('name')//选中的节目名称（方法一）
					}
				}
				if (selected != '') {
				}
				sel = selected;
			}
		}
	}),
	columns: [
		{text: '姓名',width: 100,dataIndex: 'name'},
		{text: '证件类别',width: 150,dataIndex: 'class'},
		{text: '证件号',width: 150,dataIndex: 'number'},
		{text: '出入时间',width: 150,dataIndex: 'time'},
		{text: '备注',width: 150,dataIndex: 'mark'}
	],
	dockedItems:[{
		xtype:'pagingtoolbar',
		store:nobackStore,
		dock:'bottom',
		displayInfo:true,
		displayMsg:'第{0}条到第{1}条,一共{2}条',
		emptyMsg:'没有记录'
	}],
	tbar:[{
		xtype:'button',
		text:'添加',
		handler:function(){
			var add_winForm =  Ext.create('Ext.form.Panel', {
				frame: true,   //frame属性
				width: 340,
				bodyPadding:5,
				fieldDefaults: {
					labelAlign: 'left',
					labelWidth: 90,
					anchor: '100%'
				},
				items: [{
					xtype: 'textfield', //3
					name: 'name',
					fieldLabel: '姓名',
					allowBlank: true,//必填项
					emptyText:'请输入姓名',
					blankText:"提示"//提示的内容
				},{
					xtype:'combobox',
					store:classStore,
					fieldLabel:'请选择证件类别',
					queryMode:'local',
					name:'class',
					valueField:'class',
					tpl: Ext.create('Ext.XTemplate','<tpl for=".">',
						'<div class="x-boundlist-item">{class}</div>','</tpl>'
					),
					// template for the content inside text field
					displayTpl: Ext.create('Ext.XTemplate','<tpl for=".">','{class}','</tpl>')
				},{
					//只能输入数字的文本框
					xtype: 'numberfield',
					name: 'number', //10
					fieldLabel: '证件号',
					emptyText:'请输入证件号',
					value: null
				}, {
					//日期文本框
					xtype: 'datefield', //8
					name: 'time',
					fieldLabel: '出入时间',
					value: new Date()
			},{
					xtype:'textfield',
					name:'mark',
					fieldLabel:'备注'
				}]
		});
		var syswin = Ext.create('Ext.window.Window',{
			title : "添加信息",
			width : 350,
			iconCls : "addicon",
			resizable : false,         	// 不可以随意改变大小
			draggable:false,            // 是否可以拖动
			collapsible : true,         // 允许缩放条
			closeAction : 'close',
			closable : true,
			modal : 'true',             // 弹出模态窗体
			buttonAlign : "center",
			bodyStyle : "padding:0 0 0 0",
			items : [add_winForm],
			buttons : [{
				text : "确定",
				minWidth : 70,
				handler : function() {
					var form = add_winForm.getForm();
					syswin.close();
					outsiderStore.add(form.getValues());
				}
			}, {
				text : "关闭",
				minWidth : 70,
				handler : function() {
					syswin.close();
				}
			}]
		});
		syswin.show();
		}
	},{
		text:'删除',
		handler:function(){
			del(sel);
		}
	}],
	bbar:[{
		xtype:'button',
		text:'发布',
		buttonAlign:'center',
		handler:function(){}
	}]
});

var outsider = new Ext.Panel({
	layout:'border',
	title:'外来人员管理',
	items:[outsiderGrid,outsiderHistroy]
})